﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Syntezator_mowy.Extras;
using System.Threading;

namespace Syntezator_mowy.Converter
{
    public class WavToMp3Converter
    {
        private string pathToLame;
        private SynchrBuffer<string> outBuffer;
        public bool endAction { get; set; }
        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="pathToLame">ścieżka do lame.exe</param>
        /// <param name="outBuffer">Bufor w któym przechowywane są ścieżki do plików do skonwertowania</param>
        public WavToMp3Converter(string pathToLame, ref SynchrBuffer<string> outBuffer)
        {
            this.pathToLame = pathToLame;
            this.outBuffer = outBuffer;

            this.endAction = false;
        }
        /// <summary>
        /// Metoda konwertująca plik w formacie .wav na .mp3
        /// </summary>
        /// <param name="inFile">Ścieżka do pliku .wav</param>
        /// <param name="outFile">Ścieżka do pliku .mp3</param>
        /// <param name="conversionDeviner">Argument określający przez ile należy podzielić bazowy stopień konwersji przy tej konwersji</param>
        private void convertFile(string inFile, string outFile)
        {
            ProcessStartInfo psi = new ProcessStartInfo();

            psi.UseShellExecute = false;
            psi.CreateNoWindow = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.FileName = "\"" + this.pathToLame + "\"";
            psi.Arguments = inFile + " " + outFile;

            Process p = Process.Start(psi);

            p.WaitForExit();
            p.Close();
            p.Dispose();
        }

        public void run()
        {
            while (!endAction)
            {
                string inPath = this.outBuffer.wyciagnij();

                if (inPath != default(string))
                {
                    string outPath = inPath.Split('.')[0] + ".mp3";
                    this.convertFile(inPath, outPath);
                }

                Thread.Sleep(100);
            }
            while (this.outBuffer.zwrocIlosc() > 0)
            {
                string inPath = this.outBuffer.wyciagnij();
                string outPath = inPath.Split('.')[0] + ".mp3";
                this.convertFile(inPath, outPath);
            }
        }

        public static void WavToWma(string pathToLame, string inFile)
        {
            string[] temp = inFile.Split('.');

            ProcessStartInfo psi = new ProcessStartInfo();

            psi.UseShellExecute = false;
            psi.CreateNoWindow = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.FileName = "\"" + pathToLame + "\"";
            psi.Arguments = inFile + " " + temp[0] + ".wma";

            Process p = Process.Start(psi);

        }
    }
}
